/**
 * Created by xxcao on 2017/6/6.
 */

var sql = require('./sqlMapping');
var util = require('../util/util');
var async = require('async');
var code = require('../util/code');

module.exports = {
    getBoards:function (req, res) {
        if (util.isParameterEmpty(req.query.user_id)){
            res.json(util.toJson(code.PARAMETER_IS_NULL, '参数不能为空'));
            return;
        }
        if (!util.isValidSession(req, 'GET')) {
            res.json(util.toJson(code.SESSION_VALID, 'session失效，请重新登录'));
            return;
        }
        mysqlPool.getConnection(function (err, connection) {
            if (err) {
                throw err;
            }
            var task1 = function (callback) {
                connection.query(sql.board.count, [req.query.user_id], function (err, result) {
                    if (err){
                        res.json(util.toJson(1000, '查询失败'));
                        connection.release();
                    }
                    else  {
                        if (result){
                            callback(null, result);
                        }
                        else {
                            res.json(util.toJson(1000, '查询失败'));
                            connection.release();
                        }
                    }
                });
            };
            var task2 = function (result, callback) {
                var param = null;
                var sqlStr = null;
                if (req.query.page === null || req.query.page === undefined || isNaN(Number(req.query.page))){
                    param = [req.query.user_id];
                    sqlStr = sql.board.findsAll;
                } else {
                    var pageSize = util.pageSize();
                    var page = Number(req.query.page);
                    page = Math.min(page, Math.ceil(Number(result[0]['count']) / pageSize));
                    page = Math.max(page, 1);
                    param = [req.query.user_id, pageSize, (page - 1) * pageSize];
                    sqlStr = sql.board.findsPage;
                }
                connection.query(sqlStr, param, function (err, result) {
                    if (err){
                        res.json(util.toJson(1000, '查询失败'));
                        connection.release();
                    }
                    else  {
                        if (result){
                            res.json(util.toJson(code.OPERATE_SUCCESS, '查询成功', result));
                            callback(null, result);
                        }
                        else {
                            res.json(util.toJson(1000, '查询失败'));
                            connection.release();
                        }
                    }
                });
            };
            async.waterfall([task1, task2],function (err,result) {
                if (err) {
                    var jsonString = util.toJson(1000, '查询失败');
                    res.json(jsonString);
                }
                connection.release();
            });
        })
    },
    add:function (req, res) {
        if (util.isParameterEmpty(req.body.user_id,
                req.body.board_name,
                req.body.board_remark,
                req.body.board_logo,
                req.body.phases)){
            res.json(util.toJson(code.PARAMETER_IS_NULL, '参数不能为空'));
            return;
        }
        if (!util.isValidSession(req, 'POST')) {
            res.json(util.toJson(code.SESSION_VALID, 'session失效，请重新登录'));
            return;
        }
        mysqlPool.getConnection(function (err, connection) {
            if (err) {
                throw err;
            }
            var board_id = util.getUUID();
            var task1 = function (callback) {
                connection.query(sql.board.findOne, [req.body.user_id, req.body.board_name], function (err, result) {
                    if (err){
                        res.json(util.toJson(code.CREATE_FAILURE, '创建失败'));
                        connection.release();
                    }
                    if (result && result.length > 0) {
                        res.json(util.toJson(1000, '您已存在相同名称的看板'));
                        connection.release();
                    } else {
                        callback(null, result);
                    }
                });
            };
            var task2 = function (result, callback) {
                var phases = JSON.parse(req.body.phases);
                var params = [board_id, req.body.board_name, req.body.board_remark, req.body.board_logo, phases.length, req.body.user_id];
                connection.query(sql.board.insert, params, function (err, result) {
                    if (err){
                        res.json(util.toJson(code.CREATE_FAILURE, '创建失败'));
                        connection.release();
                    }
                    if (result) {
                        callback(null,result);
                    } else {
                        res.json(util.toJson(code.CREATE_FAILURE, '创建失败'));
                        connection.release();
                    }
                })
            };
            var task3 = function (result, callback) {
                var phases = JSON.parse(req.body.phases);
                var sqls = [];
                var parameters = [];
                for (var i = 0 ; i < phases.length; i++) {
                    sqls.push(sql.board.insertPhase);
                    parameters.push([board_id, phases[i]]);
                }
                msqlQueries.queries(sqls, parameters, function(err, result){
                        if(err) {
                            var jsonString = util.toJson(code.CREATE_FAILURE, '创建失败');
                            res.json(jsonString);
                            connection.release();
                        } else {
                            callback(null, result);
                        }
                    });
            };
            var task4 = function (result, callback) {
                var members = JSON.parse(req.body.members);
                members.push(req.body.user_id); //默认加上自己
                if(members.length > 0) {
                    var sqls = [];
                    var parameters = [];
                    var i = 0;
                    for (; i < members.length; i++) {
                        sqls.push(sql.board.insertMember);
                        var isMaster = 0;
                        if (req.body.user_id === members[i]){
                            isMaster = 1;
                        }
                        parameters.push([members[i], board_id, isMaster]);
                    }
                    for (i = 0 ; i < members.length; i++) {
                        sqls.push(sql.board.insertUser2Board);
                        parameters.push([members[i], board_id]);
                    }
                    msqlQueries.queries(sqls, parameters, function(err, result){
                        if(err) {
                            var jsonString = util.toJson(code.CREATE_FAILURE, '创建失败');
                            res.json(jsonString);
                            connection.release();
                        } else {
                            res.json(util.toJson(code.OPERATE_SUCCESS, '创建看板成功'));
                            callback(null, result);
                        }
                    });
                }
                else {
                    res.json(util.toJson(code.OPERATE_SUCCESS, '创建看板成功'));
                    callback(null, result);
                }
            };
            var task5 = function (result, callback) {
                //查询成员
                var members = JSON.parse(req.body.members);
                if (members.length > 0) {
                    var param = members.join(',');
                    connection.query(sql.user.findClientIds, [param], function (err, results) {
                        if(err) {
                            res.json(util.toJson(code.DATABASE_SQL_EXCUTE_ERROR, '查询client_id失败'));
                            connection.release();
                        } else {
                            callback(null, results);
                        }
                    });
                } else {
                    callback(null, members);
                }
            };
            var task6 = function (result, callback) {
                //发送通知并写入动态表
                var client_ids = result;
                connection.query(sql.user.findUser, [req.body.user_id], function (err, results) {
                    if(err) {
                        res.json(util.toJson(code.DATABASE_SQL_EXCUTE_ERROR, '查询数据库（用户表）失败'));
                        connection.release();
                    } else {
                        if(results.length > 0) {
                            var content = results[0].user_name + "创建了看板：" + req.body.board_name;
                            var sqls = [];
                            var params = [];
                            if(client_ids.length > 0) {
                                for (var j = 0; j < client_ids.length; j++) {
                                    if(!util.isParameterEmpty(client_ids[j].client_id)) {
                                        getui.pushMessageToSingle(client_ids[j].client_id, content, content, 0, null).then(function (data) {
                                            console.log(data);
                                        }, function (err) {
                                            console.error(err);
                                        });
                                    }
                                    //插入动态消息
                                    sqls.push(sql.news.insert);
                                    params.push([util.getUUID(), content, client_ids[j].user_id, 1, board_id])
                                }
                            }
                            //插入自己进动态列表
                            sqls.push(sql.news.insert);
                            params.push([util.getUUID(), content, req.body.user_id, 1, board_id]);
                            msqlQueries.queries(sqls, params, function (err, result) {});
                        }
                        else {
                            connection.release();
                        }
                    }
                });
            };
            async.waterfall([task1, task2, task3, task4, task5, task6], function (err, result) {
                if (err) {
                    var jsonString = util.toJson(code.CREATE_FAILURE, '创建失败');
                    res.json(jsonString);
                }
                connection.release();
            });
        })
    },
    exit:function (req, res) {
        if (util.isParameterEmpty(req.body.user_id, req.body.board_id)){
            res.json(util.toJson(code.PARAMETER_IS_NULL, '参数不能为空'));
            return;
        }
        if (!util.isValidSession(req, 'POST')) {
            res.json(util.toJson(code.SESSION_VALID, 'session失效，请重新登录'));
            return;
        }
        mysqlPool.getConnection(function (err, connection) {
            if (err) {
                throw err;
            }
            new Promise(function (resolve, reject) {
               connection.query(sql.board.findPersonInGroup, [req.body.user_id, req.body.board_id], function (err, result) {
                   if (result.length > 0) {
                       resolve(result);
                   } else {
                       res.json(util.toJson(1000, '您没有当前看板，无法操作'));
                       connection.release();
                   }
               });
            }).then(function (result) {
                return new Promise(function (resolve) {
                    connection.query(sql.board.findMaster, [req.body.user_id, req.body.board_id], function (err, result) {
                        if (result && result.length > 0){
                            res.json(util.toJson(1000, '您是当前看板的管理员，只能解散看板'));
                            connection.release();
                        } else {
                            resolve(result);
                        }
                    });
                })
            }).then(function (result) {
                return new Promise(function (resolve, reject) {
                    connection.query(sql.board.deletePersonInGroup, [req.body.user_id, req.body.board_id],function (err, result) {
                        if (err) {
                            res.json(util.toJson(1000, '退出失败'));
                            connection.release();
                        } else {
                            resolve(result);
                        }
                    });
                });
            }).then(function (result) {
                return new Promise(function (resolve, reject) {
                    connection.query(sql.board.deleteGroupsPerson, [req.body.user_id, req.body.board_id], function (err, result) {
                        if (err) {
                            res.json(util.toJson(1000, '退出失败'));
                            connection.release();
                        } else {
                            res.json(util.toJson(code.OPERATE_SUCCESS, '退出成功'));
                            resolve(result);
                        }
                    });
                })
            }).then(function (result) {
                return new Promise(function (resolve, reject) {
                    connection.query(sql.board.findBoardMembersAll, [req.body.board_id], function (err, results) {
                        if (err) {
                            connection.release();
                        } else {
                            if(results.length > 0) {
                                var index = 0;
                                for (var i = 0; i < results.length; i++){
                                    if (results[i].user_id === req.body.user_id){
                                        index = i;
                                        break;
                                    }
                                }
                                var content = results[index].user_name + "退出了看板：" + results[index].board_name;
                                var params = [];
                                var sqls = [];
                                for (var j = 0; j < results.length; j++) {
                                    if (results[j].user_id !== req.body.user_id) {
                                        if (!util.isParameterEmpty(results[j].client_id)) {
                                            getui.pushMessageToSingle(results[j].client_id, content, content, 0, null).then(function (data) {
                                                console.log(data);
                                            },function (err) {
                                                console.error(err);
                                            });
                                        }
                                    }
                                    //插入动态消息
                                    sqls.push(sql.news.insert);
                                    params.push([util.getUUID(), content, results[j].user_id, 1, results[j].board_id]);
                                }
                                msqlQueries.queries(sqls, params, function (err, result) {
                                    connection.release();
                                });
                            }
                            else {
                                connection.release();
                            }
                        }
                    });
                });
            });
        })
    },
    delete:function (req, res) {
        if (util.isParameterEmpty(req.body.user_id, req.body.board_id)){
            res.json(util.toJson(code.PARAMETER_IS_NULL, '参数不能为空'));
            return;
        }
        if (!util.isValidSession(req, 'POST')) {
            res.json(util.toJson(code.SESSION_VALID, 'session失效，请重新登录'));
            return;
        }
        mysqlPool.getConnection(function (err, connection) {
            if (err) {
                throw err;
            }
            new Promise(function (resolve, reject) {
                connection.query(sql.board.findMaster, [req.body.user_id, req.body.board_id], function (err, result) {
                    if (result && result.length > 0){
                        resolve(result);
                    } else {
                        res.json(util.toJson(1000, '您不是当前看板的管理员，无法解散看板'));
                        connection.release();
                    }
                });
            }).then(function (result) {
                 return new Promise(function (resolve, reject){
                     connection.query(sql.board.deleteBoard, [req.body.board_id], function (err, result) {
                         if (err) {
                             res.json(util.toJson(1000, '解散看板失败'));
                             connection.release();
                         } else {
                             res.json(util.toJson(code.OPERATE_SUCCESS, '解散看板成功'));
                             resolve(result);
                         }
                     })
                 });
            }).then(function (result) {
                return new Promise(function (resolve, reject) {
                    connection.query(sql.board.findBoardMembersAll, [req.body.board_id], function (err, results) {
                        if (err) {
                            connection.release();
                        } else {
                            if(results.length > 0) {
                                var index = 0;
                                for (var i = 0; i < results.length; i++){
                                    if (results[i].user_id === req.body.user_id){
                                        index = i;
                                        break;
                                    }
                                }
                                var content = results[index].user_name + "解散了看板：" + results[index].board_name;
                                var params = [];
                                var sqls = [];
                                for (var j = 0; j < results.length; j++) {
                                    if (results[j].user_id !== req.body.user_id) {
                                        if (!util.isParameterEmpty(results[j].client_id)) {
                                            getui.pushMessageToSingle(results[j].client_id, content, content, 0, null).then(function (data) {
                                                console.log(data);
                                            },function (err) {
                                                console.error(err);
                                            });
                                        }
                                    }
                                    //插入动态消息
                                    sqls.push(sql.news.insert);
                                    params.push([util.getUUID(), content, results[j].user_id, 1, results[j].board_id]);
                                }
                                msqlQueries.queries(sqls, params, function (err, result) {
                                    connection.release();
                                });
                            }
                            else {
                                connection.release();
                            }
                        }
                    });
                });
            });
        })
    },
    top:function (req, res) {
        if (util.isParameterEmpty(req.body.user_id, req.body.board_id, req.body.is_top)){
            res.json(util.toJson(code.PARAMETER_IS_NULL, '参数不能为空'));
            return;
        }
        if (!util.isValidSession(req, 'POST')) {
            res.json(util.toJson(code.SESSION_VALID, 'session失效，请重新登录'));
            return;
        }
        mysqlPool.getConnection(function (err, connection) {
            if (err){
                throw err;
            }
            new Promise(function (resolve, reject) {
               connection.query(sql.board.findPersonInGroup, [req.body.user_id, req.body.board_id], function (err, result) {
                   if (result.length > 0) {
                       resolve(result);
                   } else {
                       res.json(util.toJson(1000, '您不属于当前看板，无法操作'));
                       connection.release();
                   }
               });
            }).then(function (result) {
                return new Promise(function (resolve, reject) {
                    if (isNaN(Number(req.body.is_top))) {
                        res.json(util.toJson(1000, 'is_top字段必须为数字'));
                        connection.release();
                        return;
                    }
                    var sqlStr = null;
                    if (Number(req.body.is_top) === 0) {
                        sqlStr = sql.board.updateIsNotTop;
                    } else {
                        sqlStr = sql.board.updateIsTop;
                    }
                    connection.query(sqlStr, [req.body.user_id, req.body.board_id], function (err, result) {
                        if (err === null && result) {
                            var tips = null;
                            if (Number(req.body.is_top) === 0) {
                                tips = '取消置顶成功';
                            } else {
                                tips = '置顶成功';
                            }
                            res.json(util.toJson(code.OPERATE_SUCCESS, tips));
                        } else {
                            res.json(util.toJson(1000, '操作失败'));
                        }
                        connection.release();
                    });
                });
            });
        });
    },
    disturb:function (req, res) {
        if (util.isParameterEmpty(req.body.user_id, req.body.board_id, req.body.is_disturb)){
            res.json(util.toJson(code.PARAMETER_IS_NULL, '参数不能为空'));
            return;
        }
        if (!util.isValidSession(req, 'POST')) {
            res.json(util.toJson(code.SESSION_VALID, 'session失效，请重新登录'));
            return;
        }
        mysqlPool.getConnection(function (err, connection) {
            if (err){
                throw err;
            }
            new Promise(function (resolve, reject) {
                connection.query(sql.board.findPersonInGroup, [req.body.user_id, req.body.board_id], function (err, result) {
                    if (result.length > 0) {
                        resolve(result);
                    } else {
                        res.json(util.toJson(1000, '您不属于当前看板，无法操作'));
                        connection.release();
                    }
                });
            }).then(function (result) {
                return new Promise(function (resolve, reject) {
                    if (isNaN(Number(req.body.is_disturb))) {
                        res.json(util.toJson(1000, 'is_disturb字段必须为数字'));
                        connection.release();
                        return;
                    }
                    var sqlStr = null;
                    if (Number(req.body.is_disturb) === 0) {
                        sqlStr = sql.board.updateIsNotDisturb;
                    } else {
                        sqlStr = sql.board.updateIsDisturb;
                    }
                    connection.query(sqlStr, [req.body.user_id, req.body.board_id], function (err, result) {
                        if (err === null && result) {
                            var tips = null;
                            if (Number(req.body.is_disturb) === 0) {
                                tips = '已取消免打扰';
                            } else {
                                tips = '已设为免打扰';
                            }
                            res.json(util.toJson(code.OPERATE_SUCCESS, tips));
                        } else {
                            res.json(util.toJson(1000, '操作失败'));
                        }
                        connection.release();
                    });
                });
            });
        });
    },
    detail:function (req, res) {
        if (util.isParameterEmpty(req.query.user_id, req.query.board_id)){
            res.json(util.toJson(code.PARAMETER_IS_NULL, '参数不能为空'));
            return;
        }
        if (!util.isValidSession(req, 'GET')) {
            res.json(util.toJson(code.SESSION_VALID, 'session失效，请重新登录'));
            return;
        }
        mysqlPool.getConnection(function (err, connection) {
            if (err){
                throw err;
            }
            new Promise(function (resolve, reject) {
               connection.query(sql.board.findPersonInGroup,[req.query.user_id, req.query.board_id], function (err, result) {
                   if (result.length > 0) {
                       resolve(result);
                   } else {
                       res.json(util.toJson(1000, '您不属于当前看板，无法操作'));
                       connection.release();
                   }
               });
            }).then(function (result){
                    return new Promise(function (resolve, reject) {
                        connection.query(sql.board.findBoardDetail, [req.query.board_id], function (err, result) {
                            if (err === null && result && result.length > 0) {
                                resolve(result);
                            } else {
                                res.json(util.toJson(1000, '获取详情失败'));
                                connection.release();
                            }
                        });
                    });
            }).then(function (result) {
                     return new Promise(function (resolve, reject) {
                             connection.query(sql.board.findBoardPhases, [req.query.board_id], function (err, results) {
                             if (err === null && results && results.length > 0) {
                                 var obj = {};
                                 obj.info = result[0];
                                 obj.phases = results;
                                 resolve(obj);
                             } else {
                                 res.json(util.toJson(1000, '获取详情失败'));
                                 connection.release();
                             }
                         });
                     });
            }).then(function (result) {
                    return new Promise(function (resolve, reject) {
                        connection.query(sql.board.findBoardMembers, [req.query.board_id], function (err, results) {
                            if (err === null && results) {
                                var obj = result;
                                obj.members = results;
                                resolve(obj);
                            } else {
                                res.json(util.toJson(1000, '获取详情失败'));
                                connection.release();
                            }
                        });
                    });
            }).then(function (result) {
                    return new Promise(function (resolve, reject) {
                        connection.query(sql.board.findBoardAttributes, [req.query.board_id], function (err, results) {
                            if (err === null && results) {
                                var obj = result;
                                obj.attribute = results;
                                res.json(util.toJson(code.OPERATE_SUCCESS,'获取详情成功',obj));
                                connection.release();
                            } else {
                                res.json(util.toJson(1000, '获取详情失败'));
                                connection.release();
                            }
                        });
                    });
            });
        });
    },
    addMember:function (req, res) {
        if (util.isParameterEmpty(req.body.user_id, req.body.board_id)){
            res.json(util.toJson(code.PARAMETER_IS_NULL, '参数不能为空'));
            return;
        }
        if (!util.isValidSession(req, 'POST')) {
            res.json(util.toJson(code.SESSION_VALID, 'session失效，请重新登录'));
            return;
        }
        mysqlPool.getConnection(function (err, connection) {
            if (err){
                throw err;
            }
            new Promise(function (resolve) {
                connection.query(sql.board.findPersonInGroup, [req.body.user_id, req.body.board_id], function (err, results) {
                    if (err) {
                        res.json(util.toJson(1000, '加入看板失败'));
                        connection.release();
                    } else {
                        if (results.length > 0) {
                           res.json(util.toJson(1000, '您已加入该看板'));
                           connection.release();
                        } else {
                           resolve(results);
                        }
                    }
                });
            }).then(function (result) {
                return new Promise(function (resolve) {
                    connection.query(sql.board.insertMember, [req.body.user_id, req.body.board_id, 0], function (err, result) {
                        if (err) {
                            res.json(util.toJson(1000, '加入看板失败'));
                            connection.release();
                        } else {
                            resolve(result);
                        }
                    });
                });
            }).then(function (result) {
                return new Promise(function (resolve) {
                    connection.query(sql.board.insertUser2Board, [req.body.user_id, req.body.board_id], function (err, result) {
                        if (err) {
                            res.json(util.toJson(1000, '加入看板失败'));
                            connection.release();
                        } else {
                            res.json(util.toJson(code.OPERATE_SUCCESS, '加入看板成功'));
                            resolve(result);
                        }
                    });
                });
            }).then(function (result) {
                return new Promise(function (resolve, reject) {
                    connection.query(sql.board.findBoardMembersAll, [req.body.board_id], function (err, results) {
                        if (err) {
                            connection.release();
                        } else {
                            if(results.length > 0) {
                                var index = 0;
                                for (var i = 0; i < results.length; i++){
                                    if (results[i].user_id === req.body.user_id){
                                        index = i;
                                        break;
                                    }
                                }
                                var content = results[index].user_name + "加入了看板：" + results[index].board_name;
                                var params = [];
                                var sqls = [];
                                for (var j = 0; j < results.length; j++) {
                                    if (results[j].user_id !== req.body.user_id) {
                                        if(!util.isParameterEmpty(results[j].client_id)) {
                                            getui.pushMessageToSingle(results[j].client_id, content, content, 0, null).then(function (data) {
                                                console.log(data);
                                            },function (err) {
                                                console.error(err);
                                            });
                                        }
                                    }
                                    //插入动态消息
                                    sqls.push(sql.news.insert);
                                    params.push([util.getUUID(), content, results[j].user_id, 1, results[j].board_id]);
                                }
                                msqlQueries.queries(sqls, params, function (err, result) {
                                    connection.release();
                                });
                            }
                            else {
                                connection.release();
                            }
                        }
                    });
                });
            });
        });
    },
    updateName:function (req, res) {
        //管理员操作权限
        if (util.isParameterEmpty(req.body.user_id, req.body.board_id, req.body.board_name)){
            res.json(util.toJson(code.PARAMETER_IS_NULL, '参数不能为空'));
            return;
        }
        if (!util.isValidSession(req, 'PUT')) {
            res.json(util.toJson(code.SESSION_VALID, 'session失效，请重新登录'));
            return;
        }
        mysqlPool.getConnection(function (err, connection) {
            if (err){
                throw err;
            }
            new Promise(function (resolve) {
               connection.query(sql.board.findPersonInGroup, [req.body.user_id, req.body.board_id], function (err, results) {
                   if (err === null && results && results.length > 0){
                       resolve(results);
                   } else {
                       res.json(util.toJson(1000, '您不属于当前看板，无法操作'));
                       connection.release();
                   }
               });
            }).then(function (result) {
                return new Promise(function (resolve) {
                    connection.query(sql.board.findMaster, [req.body.user_id, req.body.board_id], function (err, results) {
                        if (err === null && results && results.length > 0){
                            resolve(results);
                        } else {
                            res.json(util.toJson(1000, '您不是当前看板的管理员，无法修改名称'));
                            connection.release();
                        }
                    });
                });
            }).then(function (result) {
                return new Promise(function (resolve) {
                    connection.query(sql.board.findOne, [req.body.user_id, req.body.board_name], function (err, results) {
                        if (err) {
                            res.json(util.toJson(1000, '操作失败'));
                            connection.release();
                        } else {
                            if (results.length > 0){
                                res.json(util.toJson(1000, '已存在相同名称的看板，请修改后重试'));
                                connection.release();
                            } else {
                                resolve(results);
                            }
                        }
                    });
                });
            }).then(function (result) {
                return new Promise(function (resolve) {
                    connection.query(sql.board.updateBoardName, [req.body.board_name, req.body.board_id], function (err, result) {
                        if (err === null && result){
                            res.json(util.toJson(code.OPERATE_SUCCESS, '修改看板名称成功'));
                            resolve(result);
                        } else {
                            res.json(util.toJson(1000, '修改看板名称失败'));
                            connection.release();
                        }
                    });
                });
            }).then(function (result) {
                return new Promise(function (resolve, reject) {
                    connection.query(sql.board.findBoardMembersAll, [req.body.board_id], function (err, results) {
                        if (err) {
                            connection.release();
                        } else {
                            if(results.length > 0) {
                                var index = 0;
                                for (var i = 0; i < results.length; i++){
                                    if (results[i].user_id === req.body.user_id){
                                        index = i;
                                        break;
                                    }
                                }
                                var content = results[index].user_name + "修改了看板名称：" + results[index].board_name;
                                var params = [];
                                var sqls = [];
                                for (var j = 0; j < results.length; j++) {
                                    if (results[j].user_id !== req.body.user_id) {
                                        if (!util.isParameterEmpty(results[j].client_id)) {
                                            getui.pushMessageToSingle(results[j].client_id, content, content, 0, null).then(function (data) {
                                                console.log(data);
                                            },function (err) {
                                                console.error(err);
                                            });
                                        }
                                    }
                                    //插入动态消息
                                    sqls.push(sql.news.insert);
                                    params.push([util.getUUID(), content, results[j].user_id, 1, results[j].board_id]);
                                }
                                msqlQueries.queries(sqls, params, function (err, result) {
                                    connection.release();
                                });
                            }
                            else {
                                connection.release();
                            }
                        }
                    });
                });
            });
        });
    },
    updateRemark:function (req, res) {
        //管理员操作权限
        if (util.isParameterEmpty(req.body.user_id, req.body.board_id, req.body.board_remark)){
            res.json(util.toJson(code.PARAMETER_IS_NULL, '参数不能为空'));
            return;
        }
        if (!util.isValidSession(req, 'PUT')) {
            res.json(util.toJson(code.SESSION_VALID, 'session失效，请重新登录'));
            return;
        }
        mysqlPool.getConnection(function (err, connection) {
            if (err){
                throw err;
            }
            new Promise(function (resolve) {
                connection.query(sql.board.findPersonInGroup, [req.body.user_id, req.body.board_id], function (err, results) {
                    if (err === null && results && results.length > 0){
                        resolve(results);
                    } else {
                        res.json(util.toJson(1000, '您不属于当前看板，无法操作'));
                        connection.release();
                    }
                });
            }).then(function (result) {
                return new Promise(function (resolve) {
                    connection.query(sql.board.findMaster, [req.body.user_id, req.body.board_id], function (err, results) {
                        if (err === null && results && results.length > 0){
                            resolve(results);
                        } else {
                            res.json(util.toJson(1000, '您不是当前看板的管理员，无法修改名称'));
                            connection.release();
                        }
                    });
                });
            }).then(function (result) {
                return new Promise(function (resolve) {
                    connection.query(sql.board.updateBoardRemark, [req.body.board_remark, req.body.board_id], function (err, result) {
                        if (err === null && result){
                            res.json(util.toJson(code.OPERATE_SUCCESS, '修改看板备注成功'));
                            resolve(result);
                        } else {
                            res.json(util.toJson(1000, '修改看板备注失败'));
                            connection.release();
                        }
                    });
                });
            }).then(function (result) {
                return new Promise(function (resolve, reject) {
                    connection.query(sql.board.findBoardMembersAll, [req.body.board_id], function (err, results) {
                        if (err) {
                            connection.release();
                        } else {
                            if(results.length > 0) {
                                var index = 0;
                                for (var i = 0; i < results.length; i++){
                                    if (results[i].user_id === req.body.user_id){
                                        index = i;
                                        break;
                                    }
                                }
                                var content = results[index].user_name + "修改了看板备注：" + results[index].board_name;
                                var params = [];
                                var sqls = [];
                                for (var j = 0; j < results.length; j++) {
                                    if (results[j].user_id !== req.body.user_id) {
                                        if (!util.isParameterEmpty(results[j].client_id)) {
                                            getui.pushMessageToSingle(results[j].client_id, content, content, 0, null).then(function (data) {
                                                console.log(data);
                                            },function (err) {
                                                console.error(err);
                                            });
                                        }
                                    }
                                    //插入动态消息
                                    sqls.push(sql.news.insert);
                                    params.push([util.getUUID(), content, results[j].user_id, 1, results[j].board_id]);
                                }
                                msqlQueries.queries(sqls, params, function (err, result) {
                                    connection.release();
                                });
                            }
                            else {
                                connection.release();
                            }
                        }
                    });
                });
            });
        });
    },
    updateNoteSize:function (req, res) {
        if (util.isParameterEmpty(req.body.user_id, req.body.board_id, req.body.note_size)){
            res.json(util.toJson(code.PARAMETER_IS_NULL, '参数不能为空'));
            return;
        }
        if (!util.isValidSession(req, 'PUT')) {
            res.json(util.toJson(code.SESSION_VALID, 'session失效，请重新登录'));
            return;
        }
        mysqlPool.getConnection(function (err, connection) {
            if (err) {
              throw err;
            }
            new Promise(function (resolve) {
                connection.query(sql.board.findPersonInGroup, [req.body.user_id, req.body.board_id], function (err, result) {
                    if (err === null && result && result.length > 0){
                        resolve(result);
                    } else {
                        res.json(util.toJson(1000, '您不属于当前看板，无法操作'));
                        connection.release();
                    }
                });
            }).then(function (result) {
                if (isNaN(Number(req.body.note_size))) {
                    res.json(util.toJson(1000, 'note_size字段必须为数字'));
                    connection.release();
                    return;
                }
                connection.query(sql.board.updateNoteSize,[req.body.note_size, req.body.user_id, req.body.board_id], function (err, reuslt) {
                    if (err === null  && reuslt) {
                        res.json(util.toJson(code.OPERATE_SUCCESS, '更新看板样式成功'));
                        connection.release();
                    } else {
                        res.json(util.toJson(1000, '更新看板样式失败'));
                        connection.release();
                    }
                });
            });
        });
    }
};